const { ccclass, property } = cc._decorator;
import HeroTarget from '../SkillBuild/HeroTarget';

@ccclass
export default class SetCollisionPhysics extends cc.Component {
    //是否开启物理调试模式
    @property(cc.Boolean)
    isdebug: boolean = false;
    @property(cc.Prefab)
    heroPfb: cc.Prefab = null;
    @property(cc.Node)
    heroNode: cc.Node = null;
    @property(cc.Node)
    camere: cc.Node = null;
    @property()
    FollowX: number = 50;
    @property()
    SpeedRate: number = 0.2;

    _IsFollowing: boolean = false;
    _MixX: number = 10;
    _HeroHelper: HeroHelper = null;
    _widthScene = 0;
    _isMaxSceneX = 0;
    _heroBody: cc.Node = null;

    onLoad() {
        this._HeroHelper = HeroHelper.getInstance();
        cc.director.getCollisionManager().enabled = true;
        cc.director.getPhysicsManager().enabled = true;
        this.initHero();
    }
    initHero() {
        if (this._heroBody) {
            this._heroBody.x = 0;
            this._heroBody.y = 200;
            let control = this.heroNode.getComponent(HeroTarget);
            control.clearSkills();
        }
        else {
            this._heroBody = cc.instantiate(this.heroPfb);
            this._heroBody.group = this.heroNode.group;
            this.heroNode.addChild(this._heroBody);
            this._heroBody = this._heroBody.getChildByName("body");
            let _rigidBody = this._heroBody.getComponent(cc.RigidBody);
            _rigidBody.awake = false;
        }
    }
    onEnable() {
        if (this.isdebug) {
            cc.director.getCollisionManager().enabledDebugDraw = true;
            cc.director.getPhysicsManager().debugDrawFlags = cc.PhysicsManager.DrawBits.e_aabbBit |
                cc.PhysicsManager.DrawBits.e_jointBit |
                cc.PhysicsManager.DrawBits.e_shapeBit;
        }
    }
    onDisable() {
        cc.director.getCollisionManager().enabled = false;
        cc.director.getPhysicsManager().enabled = false;
        if (this.isdebug) {
            cc.director.getCollisionManager().enabledDebugDraw = false;
            cc.director.getPhysicsManager().debugDrawFlags = 0;
        }
    }
    lateUpdate(dt) {
        if (this._IsFollowing && this._isMaxSceneX == 0) {
            if (this._widthScene == 0) {
                this._widthScene = (this._HeroHelper.widthScene - cc.winSize.width) / 2 - 5;
            }
            let x = this._heroBody.x - this.camere.x;
            let i = 0;
            if (Math.abs(x) > this._MixX) {
                this.camere.x += ((x - this._MixX) * this.SpeedRate) * dt;
                this._HeroHelper.SetCamareX(this.camere.x);
                i++;
                if (Math.abs(this.camere.x) >= this._widthScene) {
                    if (this.camere.x > 0)
                        this._isMaxSceneX = 1;
                    else
                        this._isMaxSceneX = -1;
                }
            }
            else
                this._IsFollowing = false;
        }
        else if (this._isMaxSceneX != 0) {
            if (this._isMaxSceneX == -1) {
                let x = this._heroBody.x - this.camere.x;
                if (x > this.FollowX) {
                    this._IsFollowing = true;
                    this._isMaxSceneX = 0;
                }
            }
            else if (this._isMaxSceneX == 1) {
                let x = this._heroBody.x - this.camere.x;
                if (x < this.FollowX) {
                    this._IsFollowing = true;
                    this._isMaxSceneX = 0;
                }
            }
        }
        else {
            let x = this._heroBody.x - this.camere.x;
            if (Math.abs(x) > this.FollowX) {
                this._IsFollowing = true;
            }
        }
        this._HeroHelper.cameraPosition = this.camere.position;
    }
}
